package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.NetUtil;

/**
 * Created by Administrator on 2017/10/19.
 * 消费记录
 */

public class ConsumeReportActivity extends BaseActivity2 implements SwipeRefreshLayout.OnRefreshListener, ConsumeReportRcvAdapter.OnItemClickListener {

    private RecyclerView consume_report_irv;
    private BBswipeRefreshLayout mSwipeRefreshLayout;
    private ConsumeReportRcvAdapter consumeReportRcvAdapter;

    private EntityBean[] mConsumeReportBean;
    private LinearLayout ll_consume_null;

    private boolean mDestory;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.by_activity_consume_report;
    }

    @Override
    public void initView() {
        consume_report_irv= (RecyclerView) findViewById(R.id.consume_report_irv);
        mSwipeRefreshLayout = (BBswipeRefreshLayout) findViewById(R.id.consume_report_swipelayout);
        ll_consume_null = (LinearLayout) findViewById(R.id.ll_consume_null);
        initRec();
        initData();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    public void initRec() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        consumeReportRcvAdapter = new ConsumeReportRcvAdapter();
        consume_report_irv.setLayoutManager(new LinearLayoutManager(mContext));
        consume_report_irv.setAdapter(consumeReportRcvAdapter);
        setToolbarTitle(AppUtil.getString(R.string.consume_report));
        consumeReportRcvAdapter.setOnItemClickListener(this);
    }


    private void initData() {
        if (NetUtil.checkNetEnable()) {
            mSwipeRefreshLayout.setRefreshing(true);
            clientRequest();
        }
    }

    private void clientRequest() {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_fee_orderList2", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    parse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    @Override
    public void onRefresh() {
        clientRequest();
    }

    private void parse(Object result) {
        EntityBean bean = (EntityBean) result;
        mConsumeReportBean = (EntityBean[]) bean.get("result");
        if(mConsumeReportBean != null){
            consumeReportRcvAdapter.refreshData(mConsumeReportBean);
            ll_consume_null.setVisibility(View.GONE);
            mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        }else {
            ll_consume_null.setVisibility(View.VISIBLE);
            mSwipeRefreshLayout.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(EntityBean bean) {
        String orderid = bean.getString("out_trade_no");
        Intent consumeDetailIntent = new Intent(mContext, ConsumeDetailActivity.class);
        consumeDetailIntent.putExtra(ConsumeDetailActivity.INTENT_EXTRA_PARAMS, orderid);  //订单id
        startActivity(consumeDetailIntent);
    }
}
